#pragma once

//#include <srs_app_async_call.hpp>
#include <srs_core.hpp>
#include <srs_app_thread.hpp>

#include <string>
#include <vector>

/**
 * the async call for http hooks,
 * for the http hooks will switch st-thread,
 * so we must use isolate thread to avoid the thread corrupt,
 * for example, when dvr call http hooks, the video receive thread got
 * a video and pass it to the dvr again.
 * futhurmore, the aync call never block the main worker thread.
 */
class ISrsAsyncCallTask {
public:
    ISrsAsyncCallTask();
    virtual ~ISrsAsyncCallTask();
public:
    /**
     * execute the task async.
     * this method is the actual execute method of task,
     * for example, to notify callback server.
     */
    virtual int call() = 0;
    /**
     * convert task to string to describe it.
     * used for logger.
     */
    virtual std::string to_string() = 0;
};

/**
 * the async callback for dvr.
 * when worker call with the task, the worker will do it in isolate thread.
 * that is, the task is execute/call in async mode.
 */
class SrsAsyncCallWorker : public ISrsReusableThreadHandler {
private:
    bool running;
    SrsReusableThread* pthread;
    std::vector<ISrsAsyncCallTask*> tasks;
public:
    SrsAsyncCallWorker();
    virtual ~SrsAsyncCallWorker();
public:
    virtual int execute(ISrsAsyncCallTask* t);
public:
    virtual int start();
    virtual void stop();
    // interface ISrsReusableThreadHandler
public:
    virtual int cycle();
};
